#include "command.hpp"

extern Log lg;

int main()
{
    int shmid = CreateShm();
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);

    // 等待写入方打开之后，自己才会打开文件，向后执行, open阻塞了!
    int fd = open(FIFO_FILE, O_RDONLY);
    if (fd < 0)
    {
        lg(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }

    while (true)
    {
        char c;
        ssize_t s = read(fd, &c, 1);
        if (s == 0) break;
        else if (s < 0) break;

        cout << "client say : " << shmaddr;
        sleep(1);
    }

    shmdt(shmaddr);
    shmctl(shmid, IPC_RMID, nullptr);

    close(fd);
    return 0;
}